var ServerHandler = function(callback) {
    var that = this;

    this.socket = io.connect(g_Constants.HOST_NAME, {
        'path': "/tienlen/socket.io",
        'force new connection': true,
        'reconnect': true,
        'reconnection delay': 500,
        'reconnection limit': 2000,
        'max reconnection attempts': 1000,
        'sync disconnect on unload': false,
        'auto connect': true
    });

    this.socket.on('connect', function() {
        if (!g_VarGlobal.CONNECTED) {
            cc.log("Connected to server successfully");
            g_VarGlobal.CONNECTED = true;
            that.initReceivers();
            if (callback) {
                callback(true);
            }
        }
        //if (callback) {
        //    callback(true);
        //}
        //that.initReceivers();
    });

    this.socket.on('error', function(err) {
        cc.log("Unable to connect Socket.IO", err);
        if (err === 'handshake error') {
            if (callback) {
                callback(false);
            }
        }
    });

};

ServerHandler.prototype.initReceivers = function () {
    var that = this;

    this.socket.on("updateCookie", function (name, newDate) {
        var val = getCookie(name);
        console.log("Update cookie: " + name + " "
        + newDate + " " + val);
        setCookie(name, val, new Date(newDate));
    });

    this.socket.on('message', function (rData) {
        if (rData.success && rData.body) {
            var data = rData.body;
            switch (data.action) {
                case MessageTable.GET_CARD:
                    console.log("GET CARD from server............");
                    daifugo.playscene.getCards(rData.body.cardData);
                    break;
                case MessageTable.GET_SOLUTION:
                    console.log("GET SOLUTION from server .......");
                    setTimeout(function (){
                        daifugo.playscene.getSolution(rData.body.solution);
                    }, 500);
                    break;
                case MessageTable.GAME_STATUS:
                    daifugo.playscene.getStatus(rData.body);
                    console.log("GET STATUS from server .......");
                    break;
                case MessageTable.GET_RESULT:
                    console.log("GET RESULT from server .......");
                    setTimeout(function () {
                        daifugo.playscene.getResult(rData.body);
                    }, 2000);
                    break;
                case MessageTable.ERROR:
                    console.log("Error from server");
                    console.log(data.error);
                    var message = data.error;
                    if (message == 'Wallet of user is not enough to execute money transfer.'){
                        message = MessageNotification.NOT_ENOUGH_MONEY;
                    }
                    alert(message);
                    gotoTopScene();
                break;
            case MessageTable.LOGIN_REQUIRED:
                alert(MessageNotification.REQUEST_DEVICE_INFO_FAILED);
                location.reload();
                break;
                default :
                    break;
            }
        }
    });
};



// Function to get data from server
// Request Parameters:
// - message: message request from client
// - sData: sending data from client
// Respond Parameters:
// - message: message response from server
// - rData: responding data from server
//   + rData.body: Data to processing
//   + rData.isGood: true, false to get the status of data server
ServerHandler.prototype.contactServer = function(message, callback, sData) {
    var that = this;
    var fn_process_response = function(rData){
        if (callback){
            if (rData && rData.isgood){
                callback(rData.body);
            }
        }
        that.socket.removeListener(message, fn_process_response);
    };

    if (callback){
        that.socket.on(message, fn_process_response);
    }
    if (sData == undefined){
        that.socket.emit(message);
    }
    else {
        that.socket.emit(message, sData);
    }
};

// Dealing a set of index cards or [] to pass

// Table to get the messenger from server-client
var MessageTable = {
    DO_SMTH: "DO_SMTH",
    RE_SMTH: "RE_SMTH",
    DEAL_CARD: "DEAL_CARD",
    PASS_CARD: "PASS_CARD",
    FIND_ROOM: "find_table",
    ROOM_TALK: "ROOM_TALK",
    GET_NAME: "GET_NAME",
    GET_CARD: "GET_CARD",
    EXCHANGE_CARD: "EXCHANGE_CARD",
    GET_SOLUTION: "GET_SOLUTION",
    GET_RESULT: "GET_RESULT",
    SER_INFO: "USER_INFO",
    GAME_STATUS: "GAME_STATUS",
    LOGIN_REQUIRED: "login_required",
    ERROR: "ERROR"
};
